package controllers;

import java.util.Date;

import models.User;
import play.data.Form;
import play.data.validation.Constraints.Email;
import play.data.validation.Constraints.MinLength;
import play.data.validation.Constraints.Required;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;
import views.html.index;
import views.html.authentication.login;
import views.html.authentication.signup;

/*
 * Used for authentication and registration.
 * Cookies are used for storing the user data.
 * author: Madina Zhusupova
 */

public class Authentication extends Controller {
	
	public static Result login() {
		return ok(login.render(Form.form(Login.class)));
	}
    
    public static Result auth() {
    	Form<Login> form = Form.form(Login.class).bindFromRequest();
    	if(form.hasErrors())
    	{
    		return badRequest(login.render(form));
    	}
    	else
    	{
//    		response().setCookie("uname", form.get().username, 20000);
//    		response().setCookie("role", User.find.where().eq("username", form.get().username).findUnique().role+"", 20000);
    		session("uname", form.get().username);
    		session("userTime", String.valueOf(new Date().getTime()));
    		session("role", User.find.where().eq("username", form.get().username).findUnique().role+"");
    		flash("success", "You've been successfully logged in.");
    		return ok(index.render("Success"));
    	}
    }
    
    public static Result logout() {
//    	response().discardCookie("uname");
//    	response().discardCookie("role");
    	session().clear();
    	flash("success", "You've been successfully logged out.");
    	return redirect(routes.Authentication.login());
    }
    
    public static Result signup() {
    	return ok(signup.render(Form.form(Registration.class)));
    }
    
    /*
     * Added the new form class for registration.
     * Modified by: Mamirey Uldana
     */
    
    public static Result registration() {
    	Form<Registration> form = Form.form(Registration.class).bindFromRequest();
    	if(form.hasErrors())
    		return badRequest(signup.render(form));
    	else
    	{
    		User.create(form.get().username, form.get().password, form.get().email).save();
//    		response().setCookie("uname", form.get().username, 50000);
//          response().setCookie("role", User.find.where().eq("username", form.get().username).findUnique().role+"", 50000);

    		session("uname", form.get().username);
    		session("userTime", String.valueOf(new Date().getTime()));
    		session("role", User.find.where().eq("username", form.get().username).findUnique().role+"");
    		
    		return ok(index.render("Success"));
    	}
    }
    
    public static void cleanCartId() {
    	session().remove("cartid");
    }
    
    /*
     * Form class.
     * Wrapper class for form input fields.
     * author: Madina Zhusupova
     */
    
    public static class Login
    {
    	@Required
//    	@MinLength(8)
    	public String username;
    	@Required
//    	@MinLength(6)
    	public String password;
    	
    	public String validate()
    	{
    		if(User.authenticate(username, password) == null)
    			return "Invalid username or password";
    		return null;
    	}
    }
    
    /*
     * Extends the form inputs.
     * Added additional fields for registration form.
     * author: Mamirey Uldana
     */
    
    public static class Registration extends Login
    {
    	@Email
    	public String email;
    	@Override
    	public String validate()
    	{
    		if(User.find.where().eq("username", username).findRowCount()!=0)
    			return "Username is taken.";
    		return null;
    	}
    }
}
